﻿using System;
using System.Net.NetworkInformation;
using System.Threading.Tasks;

namespace BookSourceVerification.service.impl;

/// <summary>
/// 使用 ping 的方式测试书源url
/// </summary>
public class CheckSourcePingServiceImpl : ICheckBookSourceService
{

    public async Task<bool> Check(string url)
    {
        try
        {
            using var ping = new Ping();
            // 这个超时好像不生效？
            var pingReply = await ping.SendPingAsync(url, 200);
            return pingReply.Status == IPStatus.Success;
        }
        catch (Exception e)
        {
            throw new Exception($"网址：{url} ping 不通", e);
        }
    }
}